package com.miku.blog.expression;


import com.miku.blog.domain.pojo.LoginUser;
import com.miku.blog.domain.Role;
import com.miku.blog.domain.pojo.SystemConstance;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;


/**
 * @author shkstart
 * @create ${}YEAR-06-04 20:47
 */
@Deprecated
@Component
public class AuthenticationExpression {

    public boolean hasAuthorities(String... roles) {
        Role role = getRole();
        String roleLabel = role.getRoleLabel();
        Integer isDisable = role.getIsDisable();
        if (roleLabel.equals(SystemConstance.ROLE_USER)) {
            return false;
        }
        for (String label : roles) {
            if (roleLabel.equals(label)
                    && isDisable.equals(SystemConstance.NOT_DISABLE)) {
                return true;
            }
        }
        return false;
    }

    public boolean hasAnyAuthorities(String... roles) {
        Role role = getRole();
        String roleLabel = role.getRoleLabel();
        Integer isDisable = role.getIsDisable();
        for (String label : roles) {
            if (roleLabel.equals(label)
                    && isDisable.equals(SystemConstance.NOT_DISABLE)) {
                return true;
            }

        }
        return false;
    }

    private Role getRole() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getRole();
    }

}
